﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections;

namespace Skywave.Windows.Forms
{
    public delegate void ProfilesManagerProfileEventHandler(object sender, ProfilesManagerProfileEventArgs e);
    public delegate void ProfilesManagerConfirmDeleteEventHandler(object sender, ProfilesManagerConfirmDeleteEventArgs e);
    public partial class ProfilesManager : UserControl
    {
        public ProfilesManager()
        {
            InitializeComponent();
        }
        public event ProfilesManagerProfileEventHandler ProfileStore;
        public event ProfilesManagerProfileEventHandler ProfileLoad;
        public event ProfilesManagerConfirmDeleteEventHandler ProfileConfirmDelete;
        private Skywave.ComponentModel.ProfilesHolder mProfilesHolder;

        public void InitializeMe(string storePath)
        {
            mProfilesHolder = new Skywave.ComponentModel.ProfilesHolder(storePath);
            mProfilesHolder.ProfileStore += new Skywave.ComponentModel.ProfilesHolderProfileEventHandler(mProfilesHolder_ProfileStore);
            LoadProfiles();
        }
        public void InitializeMe(Skywave.ComponentModel.ProfilesHolder ph)
        {
            if (ph == null) return;
            mProfilesHolder = ph;
            mProfilesHolder.ProfileStore += new Skywave.ComponentModel.ProfilesHolderProfileEventHandler(mProfilesHolder_ProfileStore);
            LoadProfiles();
        }

        private void LoadProfiles()
        {
            if (mProfilesHolder == null) return;
            cmbProfiles.BeginUpdate();
            cmbProfiles.Items.Clear();
            foreach (string pName in mProfilesHolder.GetProfileNames())
            {
                cmbProfiles.Items.Add(pName);
            }
            cmbProfiles.EndUpdate();
            cmbProfiles.SelectedIndex = cmbProfiles.Items.IndexOf(mProfilesHolder.Default);
            cmbProfiles_SelectedIndexChanged(cmbProfiles, new EventArgs());
        }

        void mProfilesHolder_ProfileStore(object sender, Skywave.ComponentModel.ProfilesHolderProfileEventArgs e)
        {
            if (ProfileStore != null)
            {
                ProfilesManagerProfileEventArgs myE = new ProfilesManagerProfileEventArgs(e.Content);
                ProfileStore(this, myE);
            }
        }

        private void cmdSaveProfile_Click(object sender, EventArgs e)
        {
            if (mProfilesHolder == null || cmbProfiles.Text == "") return;
            mProfilesHolder.AddOrSave(cmbProfiles.Text);
            if (!cmbProfiles.Items.Contains(cmbProfiles.Text)) cmbProfiles.Items.Add(cmbProfiles.Text);
        }

        private void cmbProfiles_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ProfileLoad != null)
            {
                if (mProfilesHolder == null || (cmbProfiles.SelectedIndex < 0)) return;
                string pName = (string)cmbProfiles.SelectedItem;
                ProfilesManagerProfileEventArgs myE = new ProfilesManagerProfileEventArgs(mProfilesHolder.Load(pName));
                ProfileLoad(this, myE);
                mProfilesHolder.Default = pName;
            }
        }

        private void cmdRemoveProfile_Click(object sender, EventArgs e)
        {
            if (mProfilesHolder == null || cmbProfiles.Text == "") return;
            ProfilesManagerConfirmDeleteEventArgs myE = new ProfilesManagerConfirmDeleteEventArgs(false, false);
            if (ProfileConfirmDelete != null)
                ProfileConfirmDelete(this, myE);
            if (!myE.Handled)
            {
                myE.Cancel = (MessageBoxHelper.Show(string.Format(Properties.Resources.ProfilesManager_Confirm_Delete, cmbProfiles.Text), "", MessageBoxHelperButtons.YesNo, MessageBoxHelperIcon.Question, MessageBoxHelperDefaultButton.Button2, MessageBoxHelperOptions.None, this.ParentForm) == DialogResult.No);
            }
            if (!myE.Cancel)
            {
                mProfilesHolder.Delete(cmbProfiles.Text);
                if (cmbProfiles.Items.Contains(cmbProfiles.Text)) cmbProfiles.Items.Remove(cmbProfiles.Text);
            }
        }
    }
}
